﻿--
--
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'spZeusInventoryAdd' AND type = 'P')
BEGIN
  DROP PROCEDURE spZeusInventoryAdd
END
GO

-- HU - Rakomány leltár
-- EN - Inventory on package
--
CREATE PROCEDURE spZeusInventoryAdd
(@AccessToken INTEGER, @PackageCode NVARCHAR(20), @LocationCode NVARCHAR(20), @Quantity DECIMAL(18,6), @Type NVARCHAR(1))
 AS
BEGIN
  DECLARE
    @Err INTEGER,
    @Rows INTEGER,
    @UserID INTEGER,
    @Lang NVARCHAR(10)

  EXEC @Err = spAccessTokenCheck @AccessToken, @UserID OUTPUT, @Lang OUTPUT

  IF (@Err != 0)
    GOTO __ERROR__

  --
  --
  IF (@Type NOT IN (N'1', N'2')) BEGIN

    IF (@Lang = N'HU')
      RAISERROR (N'Nem jó ez a típus !', 16, 1)

    IF (@Lang = N'EN')
      RAISERROR (N'What Type is it ?', 16, 1)

    IF (@Lang = N'DE')
      RAISERROR (N'Unangemessen Typ !', 16, 1)
    
    GOTO __ERROR__
  END

  IF (@Quantity < 0) BEGIN

    IF (@Lang = N'HU')
      RAISERROR (N'Mennyiség nem lehet negatív !', 16, 1)

    IF (@Lang = N'EN')
      RAISERROR (N'Quantity could not benn less than Zero !', 16, 1)

    IF (@Lang = N'DE')
      RAISERROR (N'Menge kann nicht negativ sein !', 16, 1)

    GOTO __ERROR__
  END
  

  -- HU - Egyes számoló
  -- EN - 1st team
  --
  IF (@Type = N'1') BEGIN
  
    UPDATE ZeusInventory
        SET User1 = @UserID,
            Quantity1 = @Quantity,
            Location1 = @LocationCode
      WHERE PackageCode = @PackageCode
  
    SELECT  @Rows = @@ROWCOUNT
        
    IF (@Rows = 0) BEGIN

      INSERT INTO ZeusInventory
        (PackageCode, User1, Location1, Quantity1)
      VALUES
        (@PackageCode, @UserID, @LocationCode, @Quantity)
  
      SELECT  @Rows = @@ROWCOUNT
    END
  END    


  -- HU - Kettes számoló
  -- EN - 2nd team
  --
  IF (@Type = N'2') BEGIN
  
    UPDATE ZeusInventory
        SET User2 = @UserID,
            Quantity2 = @Quantity,
            Location2 = @LocationCode
      WHERE PackageCode = @PackageCode
  
    SELECT  @Rows = @@ROWCOUNT

    IF (@Rows = 0) BEGIN

      INSERT INTO ZeusInventory
        (PackageCode, User2, Location2, Quantity2)
      VALUES
        (@PackageCode, @UserID, @LocationCode, @Quantity)
  
      SELECT  @Rows = @@ROWCOUNT
    END
  END  


  IF (@Rows = 0) BEGIN

    IF (@Lang = N'HU')
      RAISERROR (N'Leltári adat mentése nem ment valami jól !', 16, 1)

    IF (@Lang = N'EN')
      RAISERROR (N'Inventory data storing failed !', 16, 1)

    IF (@Lang = N'DE')
      RAISERROR (N'Invertar Daten Speichern gescheitert !', 16, 1)

    GOTO __ERROR__
  END


  -- HU - Visszaadja ennek a Rakománynak az adatait
  --   A lokáció és a mennyiség az amit paraméterként kapunk !!!!!
  --
  -- EN - Get the package details
  --   Location AND Quantity MUST BE sam with parameter !!!!!
  --
  SELECT Code, ArticleCode, ArticleName, CAST(Expired as DATE) Expired, 

         @LocationCode LocationCode, @Quantity Quantity, UnitCode, N'' Notes

    FROM ZeusPackage
   WHERE Code = @PackageCode
    
-- Well done.
--
  RETURN(0)

-- We have a problem !
--
__ERROR__:
  RETURN(1)
END
GO
